package com.shrfid.rfidsdk.tag.hf;

import com.shrfid.rfidsdk.entity.RequestFrame;
import com.shrfid.rfidsdk.tag.base.BaseCommand;

import java.nio.ByteBuffer;



public class HFCommand extends BaseCommand {

    /**
     * ping 命令
     */
    public static final byte[] CMD_PING = {0X01, 0X01};

    /**
     * 写AFI指令
     */
    public static final byte[] CMD_WRITE_AFI = {(byte) 0XF0, 0X27};

    /**
     * 读多块指令
     */
    public static final byte[] CMD_READ_MULTI_BLOCK = {(byte) 0XF0, 0X23};

    /**选天线*/
    public static final byte[] CMD_SELECT_ANTENNA = {0X04,0X04};

    /**
     * set eas
     */
    public static final byte[] CMD_SET_EAS = {(byte) 0XFA, (byte) 0XA2};

    /**
     * reset eas
     */
    public static final byte[] CMD_RESET_EAS = {(byte) 0XFA, (byte) 0XA3};


    public byte[] connectDevice() {
        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_PING)
                .build().toBytes();
    }

    public byte[] inventory() {
        ByteBuffer buffer = ByteBuffer.allocate(3);
        buffer.put((byte) 0x00);
        buffer.put((byte) 0x00);
        buffer.put((byte) 0x0a);

        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_READ_MULTI_BLOCK)
                .params(buffer.array())
                .build().toBytes();
    }

    public byte[] selectAntenna(int antenna) {
        ByteBuffer buffer = ByteBuffer.allocate(1);
        buffer.put((byte) antenna);
        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_SELECT_ANTENNA)
                .params(buffer.array())
                .build().toBytes();
    }

    public byte[] writeAfi(byte[] uid, byte afi) {
        if (uid == null || uid.length != 8) {
            //TODO 参数错误抛出异常
        }
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put((byte) 0x01);
        buffer.put(uid);
        buffer.put(afi);
        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_WRITE_AFI)
                .params(buffer.array())
                .build().toBytes();
    }

    public byte[] optionPower(byte power) {
        return new byte[0];
    }

    @Override
    public byte[] setEas(byte[] uid) {
        ByteBuffer buffer = ByteBuffer.allocate(11);
        buffer.put((byte) 0x04);
        buffer.put((byte) 0x01);
        buffer.put((byte) 0x01);
        buffer.put(uid);

        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_SET_EAS)
                .params(buffer.array())
                .build().toBytes();
    }

    @Override
    public byte[] resetEas(byte[] uid) {
        ByteBuffer buffer = ByteBuffer.allocate(11);
        buffer.put((byte) 0x04);
        buffer.put((byte) 0x01);
        buffer.put((byte) 0x01);
        buffer.put(uid);

        return new HFRequestFrame.Builder()
                .address(generateRollCode())
                .cmd(CMD_RESET_EAS)
                .params(buffer.array())
                .build().toBytes();
    }


}
